<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="活动id">
              <a-input placeholder="活动id" v-model="queryParam.itemCode"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="商品名称">
              <a-input placeholder="请输入商品名称" v-model="queryParam.title"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="活动状态">
              <a-select :default-value="null" v-model="queryParam.activeStatus" placeholder="请选择活动状态"
                        @change="searchQuery">
                <a-select-option :value="null">
                  全部
                </a-select-option>
                <a-select-option :value="1">
                  未开始
                </a-select-option>
                <a-select-option :value="2">
                  进行中
                </a-select-option>
                <a-select-option :value="3">
                  已结束
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="商家名称">
              <a-input placeholder="请输入商家名称" v-model="queryParam.shopName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="8" :lg="8" :md="8" :sm="24">
            <a-form-item label="定金支付时间">
              <a-range-picker format="YYYY-MM-DD HH:mm:ss" v-model="date1" :placeholder="['开始时间', '结束时间']"
                              @change="onDate1Change"/>
            </a-form-item>
          </a-col>
          <a-col :xl="8" :lg="8" :md="8" :sm="24">
            <a-form-item label="尾款支付时间">
              <a-range-picker format="YYYY-MM-DD HH:mm:ss" v-model="date2" :placeholder="['开始时间', '结束时间']"
                              @change="onDate2Change"/>
            </a-form-item>
          </a-col>

          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button v-if="name!='B2B2C'" @click="handleAdd1" type="primary" icon="plus">新增自营商品</a-button>
      <a-divider v-if="sysMode==3||sysMode==4" type="vertical"/>
      <a-button v-if="sysMode==3||sysMode==4" @click="handleAdd2" type="primary" icon="plus">新增店铺商品</a-button>
    </div>
    <!-- table区域-begin -->
    <div>

      <a-table ref="table" size="middle" :scroll="{x:true}" bordered rowKey="id" :columns="columns"
               :dataSource="dataSource" :pagination="ipagination"
               :loading="loading" class="j-table-force-wrap"
               @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" @click="openUrlView(text)" height="40px" alt=""
               style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="activeStatus" slot-scope="text, record">
          <a-tag v-if="record.activeStatus==1" color="orange">
            未开始
          </a-tag>
          <a-tag v-if="record.activeStatus==2" color="green">
            进行中
          </a-tag>
          <a-tag v-if="record.activeStatus==3" color="red">
            已结束
          </a-tag>
        </template>
        <template slot="deposit" slot-scope="text, record">
          {{ record.deposit|numToFixed2 }}
        </template>
        <template slot="endAmount" slot-scope="text, record">
          {{ (Number(record.price) - Number(record.deposit))|numToFixed2 }}
        </template>

        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a v-if="record.activeStatus==1" @click="handleEdit(record)">编辑</a>
          <a-divider v-if="record.activeStatus==1" type="vertical"/>
          <a @click="handleDetail(record)">查看</a>
          <a-divider type="vertical"/>
          <a-popconfirm v-if="record.activeStatus!=3&&record.isExpire==0" title="确定禁用吗?"
                        @confirm="() => handleStop(record.id)">
            <a>禁用</a>
          </a-popconfirm>
          <a v-else-if="record.isExpire==1" style="color:#999999;">已禁用</a>
          <a v-else style="color:#999999;">已过期</a>
          <a-divider type="vertical"/>
          <a-popconfirm v-if="record.activeStatus!=2||record.isExpire==1" title="确定删除吗?"
                        @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
          <a-divider v-if="record.activeStatus!=1" type="vertical"/>
          <a v-if="record.activeStatus!=1" @click="handleOrders(record)">查看订单</a>

        </span>

      </a-table>
    </div>

    <yyms-good-modal ref="modalForm" @ok="modalFormOk"></yyms-good-modal>
  </a-card>
</template>

<script>
import {putAction} from '@/api/manage'
import '@/assets/less/TableExpand.less'
import {mixinDevice} from '@/utils/mixin'
import {JeecgListMixin} from '@/mixins/JeecgListMixin'
import YymsGoodModal from './modules/YymsGoodModal'
import JDate from '@/components/jeecg/JDate.vue'

export default {
  name: 'YymsGoodList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    JDate,
    YymsGoodModal
  },
  data() {
    return {
      name: process.env.VUE_APP_PATTERN,
      description: '预售商品管理页面',
      // 表头
      columns: [
        {
          title: '活动id',
          dataIndex: 'itemCode',
          align: "center",
          width: 200
        },
        {
          title: '图片',
          align: "center",
          dataIndex: 'imgUrl',
          scopedSlots: {customRender: 'imgSlot'}
        },
        {
          title: '商品名称',
          align: "center",
          dataIndex: 'title',
          width: 250,
        },
        {
          title: '商家名称',
          align: "center",
          dataIndex: 'shopName'
        },
        {
          title: '状态',
          align: "center",
          dataIndex: 'isActive',
          scopedSlots: {customRender: 'activeStatus'}
        },
        {
          title: '定金（元）',
          align: "center",
          dataIndex: 'deposit',
          scopedSlots: {customRender: 'deposit'}
        },
        {
          title: '尾款（元）',
          align: "center",
          scopedSlots: {customRender: 'endAmount'}
        },

        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          fixed: "right",
          width: 230,
          scopedSlots: {customRender: 'action'}
        }
      ],
      url: {
        list: "/mallPlus/yymsGood/list",
        delete: "/mallPlus/yymsGood/delete",
        deleteBatch: "/mallPlus/yymsGood/deleteBatch",
        exportXlsUrl: "/mallPlus/yymsGood/exportXls",
        importExcelUrl: "mallPlus/yymsGood/importExcel",

      },
      dictOptions: {},
      date1: undefined,
      date2: undefined
    }
  },
  created() {
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    searchReset() {
      this.queryParam = {}
      this.date1 = undefined
      this.date2 = undefined
      this.loadData(1)
    },
    onDate1Change: function (value, dateString) {
      this.queryParam.djTimeBegin = dateString[0]
      this.queryParam.djTimeEnd = dateString[1]
    },
    onDate2Change: function (value, dateString) {
      this.queryParam.wkTimeBegin = dateString[0]
      this.queryParam.wkTimeEnd = dateString[1]
    },
    handleAdd1: function () {
      this.$refs.modalForm.add(1)
      this.$refs.modalForm.title = "新增自营商品"
      this.$refs.modalForm.disableSubmit = false
    },
    handleAdd2: function () {
      this.$refs.modalForm.add(2)
      this.$refs.modalForm.title = "新增店铺商品"
      this.$refs.modalForm.disableSubmit = false
    },
    //禁用
    handleStop: function (id) {
      var that = this
      putAction("/mallPlus/yymsGood/stop", {id: id}).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.loadData()
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    //开启
    handleStart: function (id) {
      var that = this
      putAction("/mallPlus/yymsGood/start", {id: id}).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.loadData()
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    handleOrders(record) {
      this.$router.push({path: '/order/mng/OmsOrderPreList', query: {id: record.id}})
    },
    initDictConfig() {
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
